package mall

import (
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/gorm"
	"mall-go/common"
	mall2 "mall-go/model"
	"mall-go/repository"
	"mall-go/response"
)

/*
获取首页数据
*/

type MallIndexController struct {
	DB *gorm.DB
}

// IndexInfo 获取首页数据
//首页配置项 1-搜索框热搜 2-搜索下拉框热搜 3-(首页)热销商品 4-(首页)新品上线 5-(首页)为你推荐（configType）
func (i MallIndexController) IndexInfo(ctx *gin.Context) {
	carouselRepo := repository.NewCarouselRepo()
	indexConfigRepo := repository.NewIndexConfigRepo()
	//获取轮播图
	carousels := carouselRepo.GetCarouselsForIndex(5)
	//查询热销商品
	hotGoods := indexConfigRepo.GetConfigGoodsesForIndex(3, 4)
	//查询首页新商品
	newGoods := indexConfigRepo.GetConfigGoodsesForIndex(4, 5)
	//查询首页推荐商品
	recommendGoods := indexConfigRepo.GetConfigGoodsesForIndex(5, 10)
	response.Success(ctx, gin.H{
		"carousels":        carousels,
		"hotGoodses":       hotGoods,
		"newGoodses":       newGoods,
		"recommendGoodses": recommendGoods,
	}, "SUCCESS")
}

func NewMallIndexController() MallIndexController {
	db := common.GetDB()
	db.AutoMigrate(mall2.IndexConfig{})
	return MallIndexController{DB: db}
}
